<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary">编辑</el-button>
                  <el-button size="small" type="danger" @click.native="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :page-size="pageParams.pagesize"
                layout="prev,pager,next"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 新增弹框 -->
      <el-dialog
        title="编辑弹层"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, deleteRole, addRole } from '@/api/setting'
export default {
  name: 'VueAdminTemplateMasterSettings',

  data() {
    return {
      showDialog: false, // 控制弹窗是否显示
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      roles: [], // 角色列表
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0 // 总的数据条数
    }
  },

  created() {
    this.loadRoles()
  },

  methods: {
    // 获取角色列表
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },

    // 删除
    hDel(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteRole(id)
        console.log(res)
        // 如果当前表格中只有一条，删除之后自动请求上一页数据
        if (this.roles.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        // 删除成功后重新获取数据
        this.loadRoles()
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 添加
    async hAdd() {
      try {
        const res = await addRole(this.roleForm)
        console.log(res)
        // 添加成功后关闭弹窗
        this.showDialog = false
        // 重新获取数据
        this.loadRoles()
      } catch (error) {
        console.log(error)
      }
    },

    // 兜底校验
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        this.hAdd()
      })
    },

    handleCurrentChange(val) {
    // 跟新页码值
      console.log(`当前页: ${val}`)
      this.pageParams.page = val
      // 重新获取数据
      this.loadRoles()
    }
  }
}

</script>

<style lang="scss" scoped>

</style>

